﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using yncTech.BusinessObjects;
using yncTech.Common;

public partial class Admin_Sys_Sys002a : AdminBasePage
{
    decimal? systemId;
    decimal? functionId;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Request.QueryString[TSSystemMetadata.ColumnNames.SystemId] != null)
        {
            systemId = decimal.Parse(Request.QueryString[TSSystemMetadata.ColumnNames.SystemId]);
        }
        if(Request.QueryString[TSFunctionsMetadata.ColumnNames.FunctionID]!= null)
        {
            functionId = decimal.Parse(Request.QueryString[TSFunctionsMetadata.ColumnNames.FunctionID]);
        }
        if (!IsPostBack)
        {
            if (systemId.HasValue)
            {
                drpSystem.SelectedValue = systemId.Value.ToString();
            }
            switch (ActionType)
            {
                case OperationType.Add:
                    btnSave.Text = "新增";
                    radActive.Checked = true;
                    break;
                case OperationType.Update:
                    if (functionId.HasValue)
                    {
                        TSFunctions function = new TSFunctions();                        
                        if (function.LoadByPrimaryKey(functionId.Value))
                        {
                            drpSystem.SelectedValue = function.SystemId.Value.ToString();
                            txtChannelName.Text = function.FunctionName;
                            txtSeq.Text = function.Sequence.Value.ToString();
                            txtCode.Text = function.FunctionCode;
                            if (function.IsActive.HasValue && function.IsActive.Value)
                            {
                                radActive.Checked = true;
                            }
                            else
                            {
                                rad24.Checked = true;
                            }
                        }
                        btnSave.Text = "修改";
                    }
                    break;
                case OperationType.Delete:
                    btnSave_Click(null, null);
                    break;
            }
        }
    }

    protected void btnSave_Click(object sender, EventArgs e)
    {
        try
        {
            TSFunctions function = new TSFunctions(); 
            switch (ActionType)
            {
                case OperationType.Add:
                    function.FunctionName = txtChannelName.Text.Trim();
                    function.SystemId = decimal.Parse(drpSystem.SelectedValue);
                    function.Sequence = txtSeq.IntValue;
                    function.FunctionCode = txtCode.Text;
                    function.IsActive = radActive.Checked;
                    function.CreatedBy = LoginUserInfo.CurrentUser.LoginName;
                    function.LastUpdatedBy = LoginUserInfo.CurrentUser.LoginName;
                    function.Save();
                    break;
                case OperationType.Update:
                    function.LoadByPrimaryKey(functionId.Value);
                    function.FunctionName = txtChannelName.Text.Trim();
                    function.SystemId = decimal.Parse(drpSystem.SelectedValue);
                    function.Sequence = txtSeq.IntValue;
                    function.FunctionCode = txtCode.Text;
                    function.IsActive = radActive.Checked;
                    function.LastUpdatedBy = LoginUserInfo.CurrentUser.LoginName;
                    function.Save();
                    break;
                case OperationType.Delete:
                    function.LoadByPrimaryKey(functionId.Value);
                    function.MarkAsDeleted();
                    function.Save();
                    WebHelper.GotoParent();
                    break;
            }
            WebHelper.CloseDialog(true);
        }
        catch (Exception ex)
        {
            lbMessage.Text = ex.Message;
        }
    }

    protected void btnClose_Click(object sender, EventArgs e)
    {
        WebHelper.CloseDialog();
    }
}